package com.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class WebPage
 */
public class WebPage extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public WebPage() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			String guestIP = request.getRemoteAddr();
			//System.out.println(guestIP); //아이피 정보
			
			System.out.println("DB Access complete");
	    	
			response.setCharacterEncoding("euc-kr");
			Class.forName("oracle.jdbc.driver.OracleDriver");
			Connection conn;
			conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:XE","darkntem","jk");
			Statement st = conn.createStatement();
			ResultSet rs = st.executeQuery("select * from ACCESSIP where IP='"+guestIP+"'");
			if (rs.next()) {
				/** DB에 접속한 IP가 등록되어있음 접속 허용 **/
				System.out.println("DB : "+rs.getString(1));
				PrintWriter out = response.getWriter();
				out.println("<HTML>");
				out.println("<HEAD><TITLE>Login Success</TITLE></HEAD>");
				out.println("<BODY>");
				
				out.println("<H1>");
				out.println("<CENTER>");
				out.println("Login Success");
				out.println("</CENTER>");		
				out.println("</H1>");
				
				out.println(guestIP+" 님께서 입장하셨습니다");
				out.println("</BODY>");
				out.println("</HTML>");
				
			}else {
				/** DB에 접속한 IP가 등록되어있지 않음 접속 허용하지 않음 **/
				System.out.println("DB : Not found");
				PrintWriter out = response.getWriter();
				out.println("<HTML>");
				out.println("<HEAD><TITLE>Login failure</TITLE></HEAD>");
				out.println("<BODY>");
				
				out.println("<H1>");
				out.println("<CENTER>");
				out.println("Login failure");
				out.println("</CENTER>");	
				out.println("</H1>");
				out.println("</BODY>");
				out.println("</HTML>");
			}
	    	
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
